package com.ytzl.itrip.service.impl;

import com.ytzl.itrip.common.RedisAPI;
import com.ytzl.itrip.service.MailService;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 邮件发送
 * Created by sam on 2018/2/8.
 */
@Service("mailService")
public class MailServiceImpl implements MailService {
    @Resource
    protected MailSender mailSender;

    @Resource
    protected RedisAPI redisAPI;

    @Resource
    protected SimpleMailMessage activationMailMessage;

    @Override
    public void sendActivationMail(String mailTo, String activationCode) {
        //设置发送用户
        activationMailMessage.setTo(mailTo);
        //设置发送内容
        activationMailMessage.setText("注册邮箱:" + mailTo + " 激活编号为:" + activationCode);
        //发送
        mailSender.send(activationMailMessage);
        this.saveActivationInfo("activation:"+mailTo, activationCode);
    }

    /**
     * 保存激活code
     *
     * @param mailTo
     * @param activationCode
     */
    private void saveActivationInfo(String mailTo, String activationCode) {
        //保存时间 秒为单位 此处为30分钟
        redisAPI.set(mailTo, 30 * 60, activationCode);
    }
}
